<?php


namespace App\Admin\Extensions;


use App\Customer;
use App\Record;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class CustomerExcelExporter extends ExcelExporter implements  WithMapping
{
    protected  $fileName='客户列表.xlsx';
    protected $columns=[
        'customer_id'=>'id',
        'owner_user_id'=>'销售人员',
        'crm_ocahkr'=>'首次联系时间',
        'name'=>'客户名称',
        'mobile'=>'联系电话',
        'source'=>'信息来源',
        'crm_vfnlcv'=>'需求面积',
        'industry'=>'行业',
        'crm_sfdbto'=>'原厂区域',
        'crm_oksvoe'=>'邀约到访时间',
        'crm_nwwouj'=>'到访次数',
        'crm_cuqaob'=>'影响成交因素',
        'crm_uqdwei'=>'客户反馈问题',
        'baobei_time'=>'去电更新记录1',
        'chengjiaokehuleixing'=>'去电更新记录2',
        'record'=>'去电更新记录3',
        'create_time'=>'入客时间',
        'create_time'=>'跟进时间'
    ];
    public function map($customer) : array
    {

        $record=Record::where('types_id',$customer->customer_id)->get()->pluck('content')->toArray();
        $created=Record::where('types_id',$customer->customer_id)->get()->pluck('create_time')->toArray();

        /**
         * @var Customer $customer
         */
        return [
            $customer->customer_id ?? '',
            data_get($customer,'owner.realname') ?? '',
            $customer->crm_ocahkr ?? '',
            $customer->name ?? '',
            $customer->mobile ?? '',
            $customer->source ?? '',
            $customer->crm_vfnlcv ?? '',
            $customer->industry ?? '',
            $customer->crm_sfdbto ?? '',
            $customer->crm_oksvoe ?? '',
            $customer->crm_nwwouj ?? '',
            $customer->crm_uqdwei ?? '',
            $record[0] ?? '',
            $record[1] ?? '',
            $record[2] ?? '',
            $customer->create_time ?   \Illuminate\Support\Carbon::parse($customer->create_time)->toDateTimeString() : '',
            isset($created[0]) ?  \Illuminate\Support\Carbon::parse($created[0])->toDateTimeString() :  '',
        ];

    }
}
